import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ThymeleafConfig implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //初始化TemplateEngine
        ServletContext context = sce.getServletContext();
        //1.创建engine实例
        TemplateEngine engine = new TemplateEngine();
        //2.创建resolver实例
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(context);
        resolver.setPrefix("/WEB-INF/template/");
        resolver.setSuffix(".html");
        resolver.setCharacterEncoding("utf-8");
        engine.setTemplateResolver(resolver);
        //3.把创建好的engine实例放到ServletContext中
        context.setAttribute("engine",engine);
        System.out.println("TemplateEngine初始化");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
